﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Estsh.Core.Base;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Services;
using Estsh.Core.Repositories;
using Estsh.Core.Models;
using Newtonsoft.Json.Linq;
using Estsh.Core.Model.EnumUtil;
using Dapper;
using Estsh.Core.Model.Extend.Models;

namespace Estsh.Core.Wms.Services
{
    public class InpdlineService : BaseService<BaseEntity>, IInpdlineService
    {
        private readonly IInpdlineRepository repository;
        public InpdlineService(IInpdlineRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        public List<WmsMoveHeader> CheckMoveHeader(string onLine)
        {

            return repository.CheckMoveHeader(onLine);
        }

        public List<WmsMoveHeader> CheckOnlineMoveHeader(string onLine,string cartonno)
        {
            return repository.CheckOnlineMoveHeader(onLine,cartonno);
        }

        public List<WmsMoveDetail_Extend> CheckMoveDetail(string orderNo)
        {
            return repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);
        }

        public string UpdatePick(string loginId, string orderNo, string cartonNo, string isSplit)
        {
            //配料列表
            List<WmsMoveDetail_Extend> details = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);
            //扫描到的箱条码
            SysStock Stock = repository.GetCartonInfo(cartonNo);

            if (Stock == null)
            {
                return "[" + cartonNo + "]:箱条码不存在，请检查!";
            }
            if (Stock.Enabled.Equals("N"))
            {
                return "[" + cartonNo + "]:箱条码已被冻结，请检查!";
            }
            if (Stock.Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                return "[" + cartonNo + "]:箱条码不处于在库状态!";
            }
            if (Stock.Qty < 1)
            {
                return "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
            }

            List<WmsMoveDetail_Extend> ifScan = details.Where(a => a.PartId == Stock.PartId).ToList();
            if (ifScan.Count == 0)
            {
                return "[" + cartonNo + "]:不在领料单中，请检查！";
            }

            if (ifScan[0].FactoryId != Stock.FactoryId)
            {
                return "[" + cartonNo + "]:与领料单不在同工厂，请检查！";
            }

            if (ifScan[0].PickQty >= ifScan[0].Qty)
            {
                return "[" + ifScan[0].PartNo + "]:已完成配料！";
            }

            //获取零件号 最早批次列表
            #region FIFO 判断

            //List<SysStock> PartByFIFO = repository.GetPartByFIFO(Stock.PartId.ToString());
            //List<SysStock> isSure = PartByFIFO.Where(a => a.CartonNo == Stock.CartonNo).ToList();
            //if (isSure.Count == 0)
            //{
            //    return "[" + cartonNo + "]:箱条码不是最早批次，请扫描[" + PartByFIFO[0].CartonNo + "]";
            //}

            #endregion


            //if ((ifScan[0].PickQty + Stock.Qty) > ifScan[0].Qty && isSplit == "False")
            //{
            //    return "[" + cartonNo + "]:箱条码为尾箱超出配料数，请开启拆分进行扫描！";
            //}

            //领料 拆分 ，此功能停用
            if (isSplit == "True")
            {
                //尾箱拆分
                decimal needNum = ifScan[0].Qty - ifScan[0].PickQty; //需要拆的数量
                if (needNum >= Stock.Qty)
                {
                    //不需要拆 
                }
                else
                {
                    //原条码
                    decimal oldQty = Stock.Qty - needNum;
                    CheckMaterialSplit(Stock.CartonNo, Convert.ToInt32(oldQty), loginId, 1);
                    //更新拆分后数据
                    Stock = repository.GetCartonInfo(cartonNo);
                    return repository.UpdteStatus(ifScan, Stock, loginId);
                }
            }

            return repository.UpdteStatus(ifScan, Stock, loginId);
        }


        /// <summary>
        /// 领料（一步）扫描周转箱或箱条码
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="orderNo"></param>
        /// <param name="cartonNo"></param>
        /// <param name="isSplit"></param>
        /// <returns></returns>
        public SetObjectDetail CheckRackOrcartNo(string loginId, string orderNo, string cartonNo, string isSplit)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            //配料列表
            List<WmsMoveDetail_Extend> detailInfo = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);
            //获取单据的物料信息
            //List<WmsMoveDetail_Extend> detailInfo = repository.GetMoveDetailInfo(orderNo);

            //判断扫码的是箱条码还是周转箱
            SysStock stockInfo = repository.GetStockCartonInfo(cartonNo);
            if (stockInfo != null && stockInfo.CartonType == 1)
            {
                //获取周转箱中的物料
                List<WmsRackPackage_Extend> rackPackageInfo = repository.GetRackPackageInfo(cartonNo);

                if (rackPackageInfo.Count > 0)
                {
                    rfInfo.type = "PASS";
                    rfInfo.message = "操作成功!请选择物料输入拆分数量";
                    rfInfo.wmsObjectDetail = rackPackageInfo.ConvertAll(s => (object)s);
                }
                else
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                }
            }
            else
            {
                //获取扫描的箱条码
                SysStock Stock = repository.GetCartonInfo(cartonNo);
                if (Stock == null)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                    return rfInfo;
                }
                if (Stock.Enabled.Equals("N"))
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码已被冻结，请检查!";
                    return rfInfo;
                }
                if (Stock.Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
                    return rfInfo;
                }
                if (Stock.Qty < 1)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                    return rfInfo;
                }
                List<WmsMoveDetail_Extend> ifScan = detailInfo.Where(a => a.PartId == Stock.PartId).ToList();
                if (ifScan.Count == 0)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:不在领料单中，请检查！";
                    return rfInfo;
                }

                if (ifScan[0].FactoryId != Stock.FactoryId)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:与领料单不在同工厂，请检查！";
                    return rfInfo;
                }

                if (ifScan[0].PickQty >= ifScan[0].Qty)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + ifScan[0].PartNo + "]:已完成配料！";
                    return rfInfo;
                }

                if (detailInfo.Count != 0)
                {
                    var IsExist = detailInfo.Where(x => x.PartNo == Stock.PartNo).ToList();
                    if (IsExist.Count == 0)
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "箱条码的物料与单据物料不匹配，请检查!";
                        return rfInfo;
                    }
                    rfInfo.type = "PASS";
                    rfInfo.message = "操作成功，请输入拆分数量！";
                    rfInfo.wmsObjectDetail = null;
                    return rfInfo;
                }
                else
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "该单据中没有对应的物料信息，请检查!";
                    return rfInfo;
                }               
            }
            return rfInfo;
        }



        /// <summary>
        /// 领料（一步）输入数量拆分
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="cartNo"></param>
        /// <param name="orderNo"></param>
        /// <param name="rackNo"></param>
        /// <param name="SplitQty"></param>
        /// <param name="rackOrCartNo"></param>
        /// <returns></returns>
        public SetObjectDetail CheckSplitQty(string loginId, string cartNo, string orderNo, string SplitQty, string rackOrCartNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            int intSplitQty = Convert.ToInt32(SplitQty);

            if (string.IsNullOrEmpty(cartNo))
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "请选择列表中的箱条码!";
                return rfInfo;
            }
            //获取扫描的箱条码
            SysStock stockInfo = repository.GetCartonInfo(cartNo);
            if (stockInfo.Qty < intSplitQty)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "输入的拆分数量不能大于条码中的物料数量，请检查!";
                return rfInfo;
            }
            //获取单据的物料信息
            //List<WmsMoveDetail_Extend> detailInfo = repository.GetMoveDetailInfo(orderNo);

            //配料列表
            List<WmsMoveDetail_Extend> detailInfo = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);

            //验证选择的物料条码是否存在领料单中
            List<WmsMoveDetail_Extend> detInfo = detailInfo.Where(x => x.PartNo == stockInfo.PartNo).ToList();
            if (detInfo.Count != 0)
            {
                string partNo = detInfo.FirstOrDefault().PartNo;
                decimal pickQty = detInfo.FirstOrDefault().PickQty;
                decimal Qty = detInfo.FirstOrDefault().Qty;
                decimal residueQtyf = Qty - pickQty;
                decimal detSplitQty = Convert.ToDecimal(SplitQty);

                if (pickQty >= Qty)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "单据内配料数量已超出物料数量，请检查！";
                    return rfInfo;
                }
                if (intSplitQty > residueQtyf)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "扫描数量大于剩余配料数量，请检查！";
                    return rfInfo;
                }
                if (detInfo.FirstOrDefault().Qty >= intSplitQty)
                {
                    rfInfo.type = "PASS";
                    rfInfo.message = "请扫描目的周转箱！";
                    return rfInfo;
                }
                else
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "输入的拆分数量不能大于单据中的物料数量，请检查!";
                    return rfInfo;
                }
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "该物料条码不包含在单据内，请检查!";
                return rfInfo;
            }
        }




        /// <summary>
        ///领料（一步）扫描目的周转箱
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="rackNo"></param>
        /// <param name="isSplit"></param>
        /// <returns></returns>
        public SetObjectDetail CheckDestCartonOrRack(string orderNo, string rackOrCartNo, string cartonNo, string destRackNo, string SplitQty, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            int intSplitQty = Convert.ToInt32(SplitQty);

            //获取扫描的箱条码
            SysStock Stock = repository.GetCartonInfo(cartonNo);

            WmsMoveHeader_Extend headerInfo = repository.GetMoveHeaderInfo(orderNo);

            //判断扫码的是箱条码还是周转箱
            SysStock stockInfo = repository.GetStockCartonInfo(destRackNo);
            if (stockInfo != null && stockInfo.CartonType == 1)
            {
                //获取周转箱中的物料
                WmsRackPackage_Extend rackPackageInfo = repository.GetRackPackageInfo2(cartonNo);

                //配料列表
                List<WmsMoveDetail_Extend> detailInfo = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);

                //获取周转箱中的物料
                List<WmsRackPackage_Extend> rackPackagelist = repository.GetRackPackageInfo(cartonNo);
                if (rackPackagelist.Count > 0)
                {
                    for (int i = 0; i < rackPackagelist.Count; i++)
                    {
                        List<WmsMoveDetail_Extend> packlist = detailInfo.Where(x => x.PartNo == rackPackagelist[i].PartNo).ToList();
                        if (packlist.Count == 0)
                        {
                            rfInfo.type = "FAIL";
                            rfInfo.message = "周转箱中物料与单据中物料不一致，请检查！";
                            return rfInfo;
                        }
                    }
                }

                //验证选择的物料条码是否存在领料单中
                List<WmsMoveDetail_Extend> detInfo = detailInfo.Where(x => x.PartNo == Stock.PartNo).ToList();

                decimal oldQty = intSplitQty;
                string locateId = "";
                string locateName = "";
                if (headerInfo.prepareMaterial == 1)
                {
                    locateId = detInfo.FirstOrDefault().destLocateId;
                    locateName = detInfo.FirstOrDefault().destLocateName;
                }
                else
                {
                    //判断是否存在在途库位
                    SysLocate locateInfo = repository.GetLocateZTbin();
                    if (locateInfo == null)
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "条码对应的库存地下没有在途库位，请检查！";
                        return rfInfo;
                    }
                    locateId = locateInfo.LocateId.ToString();
                    locateName = locateInfo.LocateName;
                }


                //拆分
                CheckPickMaterialSplit(rackPackageInfo, destRackNo, locateId, locateName, rackOrCartNo, detInfo, cartonNo, Convert.ToInt32(oldQty), loginId, 1);
                
                repository.UpdteInpslineStatus( orderNo, rackPackageInfo, detInfo, stockInfo, loginId, destRackNo, SplitQty, rackOrCartNo);
                rfInfo.type = "PASS";
                rfInfo.message = "操作完成!";
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "扫描的周转箱不存在，请检查!";
                return rfInfo;
            }

            return rfInfo;
        }



        /// <summary>
        /// 领料（一步）整箱领料
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="rackOrCartNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public SetObjectDetail FullBoxPicking(string orderNo, string rackOrCartNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            //配料列表
            List<WmsMoveDetail_Extend> detailInfo = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);

            //获取周转箱中的物料
            List<WmsRackPackage_Extend> rackPackageInfo = repository.GetRackPackageInfo(rackOrCartNo);

            if (rackPackageInfo.Count > 0)
            {
                for (int i = 0; i < rackPackageInfo.Count; i++)
                {
                    decimal deRackNum = Convert.ToDecimal(rackPackageInfo[i].RackNum);
                    List<WmsMoveDetail_Extend> detailList = detailInfo.Where(x => x.PartNo == rackPackageInfo[i].PartNo).ToList();
                    if (detailList.Count == 0)
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "周转箱内的物料与单据内物料不一致，请检查!";
                        return rfInfo;
                    }

                    List<WmsMoveDetail_Extend> detailList2 = detailInfo.Where(x => x.PartNo == rackPackageInfo[i].PartNo && x.Qty == deRackNum).ToList();
                    if (detailList2.Count == 0)
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "周转箱内的物料与单据内物料数量不一致，请检查!";
                        return rfInfo;
                    }
                }
                repository.CheckFullBoxPicking( detailInfo, rackPackageInfo, loginId);
                rfInfo.type = "PASS";
                rfInfo.message = "操作成功!";
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "扫描的周转箱内不存在物料信息，请检查!";
                return rfInfo;
            }

            return rfInfo;
        }



        public SetObjectDetail CheckPickMaterialSplit(WmsRackPackage_Extend rackPackageInfo, string destRackNo, string locateId, string locateCode, string rackNo, List<WmsMoveDetail_Extend> detInfo, string cartonNo, int splitNum, string loginId, int splitCount)
        {

            SetObjectDetail det = new SetObjectDetail();
            //需要拆分的箱条码信息
            List<SysStock> MaterialInfo = repository.GetMaterialInfo(cartonNo);
            if (MaterialInfo.Count == 0)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                return det;
            }
            if (MaterialInfo[0].Enabled.Equals("N"))
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码已被冻结，请检查!";
                return det;
            }
            //if (MaterialInfo[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            //{
            //    det.type = "NO";
            //    det.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
            //    return det;
            //}
            if (MaterialInfo[0].Qty < 1)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                return det;
            }


            List<SysStock> NewStock = repository.CheckPickMaterialSplit(rackPackageInfo, destRackNo, locateId, locateCode, rackNo , detInfo, MaterialInfo, splitNum, loginId, splitCount);
            if (NewStock == null || NewStock.Count == 0)
            {
                det.type = "NO";
                det.message = "执行失败！请重试！";
            }
            else
            {
                det.type = "OK";
                det.wmsObjectDetail = NewStock.ConvertAll(s => (object)s);
            }
            return det;
        }





        public string BatchingCompleted(string loginId, string orderNo)
        {
            return repository.BatchingCompleted(loginId, orderNo);
        }

        public SetObjectDetail CheckMaterialSplit(string cartonNo, int splitNum, string loginId, int splitCount)
        {

            SetObjectDetail det = new SetObjectDetail();
            //需要拆分的箱条码信息
            List<SysStock> MaterialInfo = repository.GetMaterialInfo(cartonNo);
            if (MaterialInfo.Count == 0)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                return det;
            }
            if (MaterialInfo[0].Enabled.Equals("N"))
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码已被冻结，请检查!";
                return det;
            }
            //if (MaterialInfo[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            //{
            //    det.type = "NO";
            //    det.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
            //    return det;
            //}
            if (MaterialInfo[0].Qty < 1)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                return det;
            }


            List<SysStock> NewStock = repository.CheckMaterialSplit(MaterialInfo, splitNum, loginId, splitCount);
            if (NewStock == null || NewStock.Count == 0)
            {
                det.type = "NO";
                det.message = "执行失败！请重试！";
            }
            else
            {
                det.type = "OK";
                det.wmsObjectDetail = NewStock.ConvertAll(s => (object)s);
            }
            return det;
        }

        public List<WmsMoveDetail> CheckMoveDetail2(string orderNo)
        {
            return repository.CheckMoveDetail2(orderNo, (int)WmsEnumUtil.StockStatus.BATCHED);
        }

        public string CheckLocateStatus(string locate, string orderNo)
        {
            List<SysLocate> locateInfo = repository.GetLocateInfo(locate);
            if (locateInfo == null || locateInfo.Count == 0)
            {
                return "[" + locate + "]:库位不存在，请检查!";
            }

            if (locateInfo[0].LocateType != (int)WmsEnumUtil.LocateType.LINE_LOCATE)
            {
                return "[" + locate + "]:库位不是线边库，请检查!";
            }

            //已使用库容
            List<SysStock> s = repository.GetLocateCapacity(locateInfo[0].LocateId.ToString());
            if (locateInfo[0].LocateCapacity <= s[0].Qty)
            {
                return "[" + locate + "]:线边库已满，请检查!";
            }

            List<WmsMoveDetail_Extend> details = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);
            if (locateInfo[0].ZoneId == details[0].DestZoneId && locateInfo[0].WarehouseId == details[0].DestWarehouseId && locateInfo[0].ErpWarehouse == details[0].DestErpWarehouse)
            {
                return "OK";
            }
            else
            {
                return "[" + locate + "]:库位与订单目标库位不匹配，请检查!";
            }
            //return "OK";

        }

        public string UpdatePickOnline(string loginId, string orderNo, string cartonNo, string locate)
        {

            //上料列表
            List<WmsMoveSn> details = repository.GetMoveSN(orderNo);
            //扫描到的箱条码
            SysStock Stock = repository.GetCartonInfo(cartonNo);
            //库位
            List<SysLocate> locateInfo = repository.GetLocateInfo(locate);
            if (Stock == null)
            {
                return "[" + cartonNo + "]:箱条码不存在，请检查!";
            }
            if (Stock.Enabled.Equals("N"))
            {
                return "[" + cartonNo + "]:箱条码已被冻结，请检查!";
            }

            if (Stock.Status == (int)WmsEnumUtil.StockStatus.ONLINED)
            {
                return "[" + cartonNo + "]:箱条码已完成上线!";
            }

            if (Stock.Status != (int)WmsEnumUtil.StockStatus.BATCHED)
            {
                return "[" + cartonNo + "]:箱条码不处于配料状态!";
            }

            List<WmsMoveSn> ifOnline = details.Where(a => a.CartonNo == cartonNo).ToList();
            if (ifOnline.Count == 0)
            {
                return "[" + cartonNo + "]:不在领料单中，请检查！";
            }
            if (ifOnline[0].FactoryId != Stock.FactoryId)
            {
                return "[" + cartonNo + "]:与领料单不在同工厂，请检查！";
            }
            if (ifOnline[0].PartNo != locateInfo[0].PartNo)
            {
                return "[" + ifOnline[0].PartNo + "]零件与[" + locate + "]不匹配，请检查！";
            }


            return repository.UpdteStatusOnLine(loginId, orderNo, Stock, locateInfo);
        }



        public List<WmsPullingOrder> GetPullingOrderList(string Line, string MaterialType)
        {
            return repository.GetPullingOrderList(Line, MaterialType);
        }

        public List<WmsPullingOrderDetail> GetPullingOrderDetails(string orderNo)
        {
            return repository.GetPullingOrderDetails(orderNo);
        }

        public string CheckRK(string RKNO, string orderNo, string loginId, string isUpdate, string UpdateNum)
        {
            List<SysStock> stock = repository.GetCartonInfoByRK(RKNO);

            //判断库内有此条码记录
            if (stock.Count > 0)
            {
                //判断此条码在库内为 50 在库状态
                if (stock.Where(a => a.Status == (int)WmsEnumUtil.StockStatus.INSTOCKED).Count() > 0)
                {
                    //判断为料架号
                    if (stock[0].CartonType == 1)
                    {
                        List<WmsRack> rkInfos = repository.GetRkInfos(RKNO);
                        if (rkInfos.Count == 0)
                        {
                            return "[" + RKNO + "]:料架号不存在，请检查!";
                        }
                        if (rkInfos[0].Enabled != WmsEnumUtil.Enabled.Y.ToString())
                        {
                            return "[" + RKNO + "]:料架号为冻结状态，请检查!";
                        }

                        List<SysStock> RackPackages = repository.GetRackPackage(RKNO);
                        return UpdateStatusByRKorCarton(RackPackages, orderNo, loginId, RKNO, isUpdate, UpdateNum);
                    }
                    else
                    {
                        //判断为箱条码
                        SysStock Stock = repository.GetCartonInfo(RKNO);

                        if (Stock == null)
                        {
                            return "[" + RKNO + "]:箱条码不存在，请检查!";
                        }
                        if (Stock.Enabled.Equals("N"))
                        {
                            return "[" + RKNO + "]:箱条码已被冻结，请检查!";
                        }
                        if (Stock.Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
                        {
                            return "[" + RKNO + "]:箱条码不处于在库状态!";
                        }
                        if (Stock.Qty < 1)
                        {
                            return "[" + RKNO + "]:箱条码数量不足，无法拆分!";
                        }
                        List<SysStock> RackPackages = new List<SysStock>();
                        RackPackages.Add(Stock);
                        return UpdateStatusByRKorCarton(RackPackages, orderNo, loginId, RKNO, isUpdate, UpdateNum);
                    }
                }
                else
                {
                    return "[" + RKNO + "]:条码不在库内，请检查!";
                }
            }
            else
            {
                return "[" + RKNO + "]:在库存中未找到条码，请检查!";
            }

        }

        private string UpdateStatusByRKorCarton(List<SysStock> RackPackages, string orderNo, string loginId, string RKNO, string isUpdate, string UpdateNum)
        {
            //料架内物料信息列表 
            if (RackPackages.Count > 0)
            {
                //查找线边库
                List<SysPdline> pdlineLocate = repository.GetPdline(orderNo);
                if (pdlineLocate == null || pdlineLocate.Count == 0)
                {
                    return "[" + RKNO + "]:未找到产线对应的线边库，请检查!";
                }
                //大件拣选订单
                List<WmsPullingOrderDetail> wmsPullingOrderDetails = repository.GetPullingOrderDetails(orderNo);
                List<WmsPullingOrderDetail> is10 = wmsPullingOrderDetails.Where(a => a.ItemStatus == 10).OrderBy(b => b.WorkOrderSeq).ToList();

                //判断 料架物料 与 订单物料 是否匹配   
                List<SysStock> isOk = RackPackages.Where(a => a.PartId == is10[0].PartId).OrderBy(b => b.Qty).ToList();

                if (isOk.Count > 0)
                {
                    //料架车 A物料已有数量
                    int RKsum = Convert.ToInt32(isOk.Sum(s => s.Qty));
                    //拣选订单 A物料总拣选数量
                    int PickingNum = 0;
                    for (int i = 0; i < is10.Count; i++)
                    {
                        if (is10[0].PartId == is10[i].PartId)
                        {
                            PickingNum += Convert.ToInt32(is10[i].Qty);
                        }
                        else
                        {
                            break;
                        }
                    }

                    int UseNum = RKsum > PickingNum ? PickingNum : RKsum; //使用小的值用来循环

                    if (isUpdate == "True")
                    {
                        int SetUpdateNum = Convert.ToInt32(UpdateNum);
                        if (SetUpdateNum <= UseNum)
                        {
                            UseNum = SetUpdateNum;
                        }
                        else
                        {
                            return "[" + UpdateNum + "]:数量超过订单序列数量，请检查!";
                        }
                    }

                    int DetailNum = UseNum;
                    int StockNum = UseNum;
                    //SQL操作
                    List<string> sqlStrings = new List<string>();
                    List<DynamicParameters> parameterList = new List<DynamicParameters>();

                    //修改主表状态
                    StringBuilder updateString0 = new StringBuilder(1024);
                    updateString0.Append(" update [wms_pulling_order] set order_status='20' where order_no=@order_no ");
                    DynamicParameters updateStringparameters0 = new DynamicParameters();
                    updateStringparameters0.Add("@order_no", is10[0].OrderNo);
                    updateStringparameters0.Add("@update_userid", loginId);
                    updateStringparameters0.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString0.ToString());
                    parameterList.Add(updateStringparameters0);

                    //拣选单明细处理 UseNum 个
                    while (DetailNum > 0)
                    {
                        for (int i = 0; i < is10.Count; i++)
                        {
                            if (is10[i].Qty <= DetailNum)
                            {
                                StringBuilder updateString = new StringBuilder(1024);
                                updateString.Append(" update [wms_pulling_order_detail] set item_status='20' where guid=@guid ");
                                DynamicParameters updateStringparameters = new DynamicParameters();
                                updateStringparameters.Add("@guid", is10[i].Guid);
                                updateStringparameters.Add("@update_userid", loginId);
                                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                                sqlStrings.Add(updateString.ToString());
                                parameterList.Add(updateStringparameters);

                                DetailNum -= Convert.ToInt32(is10[i].Qty);
                            }
                            //拣选订单明细行 默认数量都是一个
                        }
                    }
                    //库存处理 UseNum 个
                    while (StockNum > 0)
                    {
                        for (int i = 0; i < isOk.Count; i++)
                        {
                            if (isOk[i].Qty <= StockNum)
                            {
                                StringBuilder updateString5 = new StringBuilder(1024);
                                updateString5.Append("update sys_stock set  status=@status,update_userid=@update_userid,update_time=@update_time,locate_id=@locate_id,locate_name=@locate_name,zone_id=@zone_id,zone_name=@zone_name   where carton_no= @carton_no ");
                                DynamicParameters updateStringparameters5 = new DynamicParameters();
                                updateStringparameters5.Add("@carton_no", isOk[i].CartonNo);
                                updateStringparameters5.Add("@status", (int)WmsEnumUtil.StockStatus.ONLINED);
                                updateStringparameters5.Add("@locate_id", pdlineLocate[i].SrcLocateId);
                                updateStringparameters5.Add("@locate_name", pdlineLocate[i].SrcLocateName);
                                updateStringparameters5.Add("@zone_id", pdlineLocate[i].SrcZoneId);
                                updateStringparameters5.Add("@zone_name", pdlineLocate[i].SrcZoneName);
                                updateStringparameters5.Add("@update_userid", loginId);
                                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                                sqlStrings.Add(updateString5.ToString());
                                parameterList.Add(updateStringparameters5);

                                StockNum -= Convert.ToInt32(isOk[i].Qty);

                                //新增 sys_stock_trans 事务表
                                #region sys_stock_trans 事务表

                                StringBuilder updateString6 = new StringBuilder(1024);
                                updateString6.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                                DynamicParameters updateStringparameters6 = new DynamicParameters();
                                updateStringparameters6.Add("@trans_code", (int)WmsEnumUtil.TransType.PICK_ONLINE);
                                updateStringparameters6.Add("@carton_no", isOk[i].CartonNo);
                                updateStringparameters6.Add("@part_id", isOk[i].PartId);
                                updateStringparameters6.Add("@part_no", isOk[i].PartNo);
                                updateStringparameters6.Add("@part_spec", isOk[i].PartSpec);
                                updateStringparameters6.Add("@src_locate_id", isOk[i].LocateId);
                                updateStringparameters6.Add("@src_locate_name", isOk[i].LocateName);
                                updateStringparameters6.Add("@dest_locate_id", pdlineLocate[i].SrcLocateId);
                                updateStringparameters6.Add("@dest_locate_name", pdlineLocate[i].SrcLocateName);
                                updateStringparameters6.Add("@old_qty", isOk[i].Qty);
                                updateStringparameters6.Add("@new_qty", isOk[i].Qty);
                                updateStringparameters6.Add("@trans_qty", isOk[i].Qty);
                                updateStringparameters6.Add("@old_status", isOk[i].Status);
                                updateStringparameters6.Add("@new_status", (int)WmsEnumUtil.StockStatus.ONLINED);
                                updateStringparameters6.Add("@old_qms_status", isOk[i].QmsStatus);
                                updateStringparameters6.Add("@new_qms_status", isOk[i].QmsStatus);
                                updateStringparameters6.Add("@unit", isOk[i].Unit);
                                updateStringparameters6.Add("@factory_id", isOk[i].FactoryId);
                                updateStringparameters6.Add("@factory_code", isOk[i].FactoryCode);
                                updateStringparameters6.Add("@src_erp_warehouse", isOk[i].ErpWarehouse);
                                updateStringparameters6.Add("@dest_erp_warehouse", isOk[i].ErpWarehouse);
                                updateStringparameters6.Add("@src_warehouse_id", isOk[i].WarehouseId);
                                updateStringparameters6.Add("@src_warehouse_name", isOk[i].WarehouseName);
                                updateStringparameters6.Add("@dest_warehouse_id", isOk[i].WarehouseId);
                                updateStringparameters6.Add("@dest_warehouse_name", isOk[i].WarehouseName);
                                updateStringparameters6.Add("@src_zone_id", isOk[i].ZoneId);
                                updateStringparameters6.Add("@src_zone_name", isOk[i].ZoneName);
                                updateStringparameters6.Add("@dest_zone_id", pdlineLocate[i].SrcZoneId);
                                updateStringparameters6.Add("@dest_zone_name", pdlineLocate[i].SrcZoneName);
                                updateStringparameters6.Add("@enabled", WmsEnumUtil.Enabled.N.ToString());
                                updateStringparameters6.Add("@update_userid", loginId);
                                updateStringparameters6.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                                updateStringparameters6.Add("@create_userid", loginId);
                                updateStringparameters6.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                                updateStringparameters6.Add("@guid", Guid.NewGuid());

                                sqlStrings.Add(updateString6.ToString());
                                parameterList.Add(updateStringparameters6);
                                #endregion
                            }
                            else
                            {
                                StringBuilder updateString5 = new StringBuilder(1024);
                                updateString5.Append("update sys_stock set qty=@qty, update_userid=@update_userid,update_time=@update_time   where carton_no= @carton_no ");
                                DynamicParameters updateStringparameters5 = new DynamicParameters();
                                updateStringparameters5.Add("@carton_no", isOk[i].CartonNo);
                                updateStringparameters5.Add("@qty", isOk[i].Qty - StockNum);
                                updateStringparameters5.Add("@update_userid", loginId);
                                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                                sqlStrings.Add(updateString5.ToString());
                                parameterList.Add(updateStringparameters5);

                                //新增 sys_stock_trans 事务表
                                #region sys_stock_trans 事务表

                                StringBuilder updateString8 = new StringBuilder(1024);
                                updateString8.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                                DynamicParameters updateStringparameters8 = new DynamicParameters();
                                updateStringparameters8.Add("@trans_code", (int)WmsEnumUtil.TransType.PICK_ONLINE);
                                updateStringparameters8.Add("@carton_no", isOk[i].CartonNo);
                                updateStringparameters8.Add("@part_id", isOk[i].PartId);
                                updateStringparameters8.Add("@part_no", isOk[i].PartNo);
                                updateStringparameters8.Add("@part_spec", isOk[i].PartSpec);
                                updateStringparameters8.Add("@src_locate_id", isOk[i].LocateId);
                                updateStringparameters8.Add("@src_locate_name", isOk[i].LocateName);
                                updateStringparameters8.Add("@dest_locate_id", isOk[i].LocateId);
                                updateStringparameters8.Add("@dest_locate_name", isOk[i].LocateName);
                                updateStringparameters8.Add("@old_qty", isOk[i].Qty);
                                updateStringparameters8.Add("@new_qty", isOk[i].Qty - StockNum);
                                updateStringparameters8.Add("@trans_qty", isOk[i].Qty);
                                updateStringparameters8.Add("@old_status", isOk[i].Status);
                                updateStringparameters8.Add("@new_status", isOk[i].Status);
                                updateStringparameters8.Add("@old_qms_status", isOk[i].QmsStatus);
                                updateStringparameters8.Add("@new_qms_status", isOk[i].QmsStatus);
                                updateStringparameters8.Add("@unit", isOk[i].Unit);
                                updateStringparameters8.Add("@factory_id", isOk[i].FactoryId);
                                updateStringparameters8.Add("@factory_code", isOk[i].FactoryCode);
                                updateStringparameters8.Add("@src_erp_warehouse", isOk[i].ErpWarehouse);
                                updateStringparameters8.Add("@dest_erp_warehouse", isOk[i].ErpWarehouse);
                                updateStringparameters8.Add("@src_warehouse_id", isOk[i].WarehouseId);
                                updateStringparameters8.Add("@src_warehouse_name", isOk[i].WarehouseName);
                                updateStringparameters8.Add("@dest_warehouse_id", isOk[i].WarehouseId);
                                updateStringparameters8.Add("@dest_warehouse_name", isOk[i].WarehouseName);
                                updateStringparameters8.Add("@src_zone_id", isOk[i].ZoneId);
                                updateStringparameters8.Add("@src_zone_name", isOk[i].ZoneName);
                                updateStringparameters8.Add("@dest_zone_id", isOk[i].ZoneId);
                                updateStringparameters8.Add("@dest_zone_name", isOk[i].ZoneName);
                                updateStringparameters8.Add("@enabled", isOk[i].Enabled);
                                updateStringparameters8.Add("@update_userid", loginId);
                                updateStringparameters8.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                                updateStringparameters8.Add("@create_userid", loginId);
                                updateStringparameters8.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                                updateStringparameters8.Add("@guid", Guid.NewGuid());

                                sqlStrings.Add(updateString8.ToString());
                                parameterList.Add(updateStringparameters8);
                                #endregion


                                #region 插入拆分出的箱条码

                                isOk[i].CartonNo = repository.checkCartonNo(isOk[i].CartonNo, 1);


                                StringBuilder updateString3 = new StringBuilder(1024);
                                updateString3.Append(@" INSERT INTO [dbo].[sys_stock]
                                        ([vendor_id]
                                        ,[vendor_code]
                                        ,[carton_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[lot_no]
                                        ,[fix_lot_no]
                                        ,[status]
                                        ,[qty]
                                        ,[snp_qty]
                                        ,[locate_id]
                                        ,[locate_name]
                                        ,[group_no]
                                        ,[erp_warehouse]
                                        ,[date_code]
                                        ,[qms_status]
                                        ,[ref_order_no]
                                        ,[unit]
                                        ,[dock]
                                        ,[warehouse_id]
                                        ,[warehouse_name]
                                        ,[zone_id]
                                        ,[zone_name]
                                        ,[printed]
                                        ,[print_time]
                                        ,[remark]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                        VALUES(@vendor_id,@vendor_code,@carton_no,@part_id,@part_no,@part_spec,@lot_no,@fix_lot_no,@status,@qty,@snp_qty
,@locate_id,@locate_name,@group_no,@erp_warehouse,@date_code,@qms_status,@ref_order_no,@unit,@dock,@warehouse_id,@warehouse_name,@zone_id,@zone_name
,@printed,@print_time,@remark,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid )");

                                DynamicParameters updateStringparameters3 = new DynamicParameters();
                                updateStringparameters3.Add("@vendor_id", isOk[i].VendorId);
                                updateStringparameters3.Add("@vendor_code", isOk[i].VendorCode);
                                updateStringparameters3.Add("@carton_no", isOk[i].CartonNo);
                                updateStringparameters3.Add("@part_id", isOk[i].PartId);
                                updateStringparameters3.Add("@part_no", isOk[i].PartNo);
                                updateStringparameters3.Add("@part_spec", isOk[i].PartSpec);
                                updateStringparameters3.Add("@lot_no", isOk[i].LotNo);
                                updateStringparameters3.Add("@fix_lot_no", isOk[i].FixLotNo);
                                updateStringparameters3.Add("@status", (int)WmsEnumUtil.StockStatus.ONLINED);
                                updateStringparameters3.Add("@qty", StockNum);
                                updateStringparameters3.Add("@snp_qty", isOk[i].SnpQty);
                                updateStringparameters3.Add("@locate_id", pdlineLocate[0].SrcLocateId);
                                updateStringparameters3.Add("@locate_name", pdlineLocate[0].SrcLocateName);
                                updateStringparameters3.Add("@group_no", isOk[i].GroupNo);
                                updateStringparameters3.Add("@erp_warehouse", isOk[i].ErpWarehouse);
                                updateStringparameters3.Add("@date_code", isOk[i].DateCode);
                                updateStringparameters3.Add("@qms_status", isOk[i].QmsStatus);
                                updateStringparameters3.Add("@ref_order_no", isOk[i].RefOrderNo);
                                updateStringparameters3.Add("@unit", isOk[i].Unit);
                                updateStringparameters3.Add("@dock", isOk[i].Dock);
                                updateStringparameters3.Add("@warehouse_id", isOk[i].WarehouseId);
                                updateStringparameters3.Add("@warehouse_name", isOk[i].WarehouseName);
                                updateStringparameters3.Add("@zone_id", pdlineLocate[0].SrcZoneId);
                                updateStringparameters3.Add("@zone_name", pdlineLocate[0].SrcZoneName);
                                updateStringparameters3.Add("@printed", isOk[i].Printed);
                                updateStringparameters3.Add("@print_time", isOk[i].PrintTime);
                                updateStringparameters3.Add("@remark", isOk[i].Remark);
                                updateStringparameters3.Add("@factory_id", isOk[i].FactoryId);
                                updateStringparameters3.Add("@factory_code", isOk[i].FactoryCode);
                                updateStringparameters3.Add("@enabled", isOk[i].Enabled);
                                updateStringparameters3.Add("@update_userid", loginId);
                                updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                                updateStringparameters3.Add("@create_userid", isOk[i].CreateUserId);
                                updateStringparameters3.Add("@create_time", isOk[i].CreateTime);
                                updateStringparameters3.Add("@guid", Guid.NewGuid());

                                sqlStrings.Add(updateString3.ToString());
                                parameterList.Add(updateStringparameters3);
                                #endregion
                                //新增 sys_stock_trans 事务表
                                #region sys_stock_trans 事务表

                                StringBuilder updateString6 = new StringBuilder(1024);
                                updateString6.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                                DynamicParameters updateStringparameters6 = new DynamicParameters();
                                updateStringparameters6.Add("@trans_code", (int)WmsEnumUtil.TransType.PICK_ONLINE);
                                updateStringparameters6.Add("@carton_no", isOk[i].CartonNo);
                                updateStringparameters6.Add("@part_id", isOk[i].PartId);
                                updateStringparameters6.Add("@part_no", isOk[i].PartNo);
                                updateStringparameters6.Add("@part_spec", isOk[i].PartSpec);
                                updateStringparameters6.Add("@src_locate_id", isOk[i].LocateId);
                                updateStringparameters6.Add("@src_locate_name", isOk[i].LocateName);
                                updateStringparameters6.Add("@dest_locate_id", pdlineLocate[0].SrcLocateId);
                                updateStringparameters6.Add("@dest_locate_name", pdlineLocate[0].SrcLocateName);
                                updateStringparameters6.Add("@old_qty", 0);
                                updateStringparameters6.Add("@new_qty", StockNum);
                                updateStringparameters6.Add("@trans_qty", isOk[i].Qty);
                                updateStringparameters6.Add("@old_status", isOk[i].Status);
                                updateStringparameters6.Add("@new_status", (int)WmsEnumUtil.StockStatus.ONLINED);
                                updateStringparameters6.Add("@old_qms_status", isOk[i].QmsStatus);
                                updateStringparameters6.Add("@new_qms_status", isOk[i].QmsStatus);
                                updateStringparameters6.Add("@unit", isOk[i].Unit);
                                updateStringparameters6.Add("@factory_id", isOk[i].FactoryId);
                                updateStringparameters6.Add("@factory_code", isOk[i].FactoryCode);
                                updateStringparameters6.Add("@src_erp_warehouse", isOk[i].ErpWarehouse);
                                updateStringparameters6.Add("@dest_erp_warehouse", isOk[i].ErpWarehouse);
                                updateStringparameters6.Add("@src_warehouse_id", isOk[i].WarehouseId);
                                updateStringparameters6.Add("@src_warehouse_name", isOk[i].WarehouseName);
                                updateStringparameters6.Add("@dest_warehouse_id", isOk[i].WarehouseId);
                                updateStringparameters6.Add("@dest_warehouse_name", isOk[i].WarehouseName);
                                updateStringparameters6.Add("@src_zone_id", isOk[i].ZoneId);
                                updateStringparameters6.Add("@src_zone_name", isOk[i].ZoneName);
                                updateStringparameters6.Add("@dest_zone_id", pdlineLocate[0].SrcZoneId);
                                updateStringparameters6.Add("@dest_zone_name", pdlineLocate[0].SrcZoneName);
                                updateStringparameters6.Add("@enabled", isOk[i].Enabled);
                                updateStringparameters6.Add("@update_userid", loginId);
                                updateStringparameters6.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                                updateStringparameters6.Add("@create_userid", loginId);
                                updateStringparameters6.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                                updateStringparameters6.Add("@guid", Guid.NewGuid());

                                sqlStrings.Add(updateString6.ToString());
                                parameterList.Add(updateStringparameters6);
                                #endregion


                                StockNum = 0;
                            }
                        }
                    }

                    return repository.UpdateLargePick(sqlStrings, parameterList);
                }
                else
                {
                    return "[" + RKNO + "]:不匹配拣选列表，请检查!";
                }
            }
            else
            {
                return "[" + RKNO + "]:未找到料架对应包装关系，请检查!";
            }
        }

        public string CheckMaterial(string cartonNo, string orderNo, string isUpdate, string loginId)
        {
            List<WmsPullingOrderDetail> wmsPullingOrderDetails = repository.GetPullingOrderDetails(orderNo);
            List<WmsPullingOrderDetail> istrue = wmsPullingOrderDetails.Where(a => a.PartNo.Equals(cartonNo)).ToList();
            if (istrue.Count == 0)
            {
                return "[" + cartonNo + "]:物料号不存在，请检查!";
            }

            List<WmsPullingOrderDetail> isN = wmsPullingOrderDetails.Where(a => a.ItemStatus == 10).OrderBy(b => b.WorkOrderSeq).ToList();
            if (isN[0].PartNo == cartonNo)
            {
                if (isUpdate == "True")
                {
                    return "OK";
                }
                else
                {
                    //判断未完成的列表，扫描的物料是不是第一个
                    List<WmsPullingOrderDetail> setOne = new List<WmsPullingOrderDetail>();
                    setOne.Add(isN[0]);
                    return repository.UpdatePullingOrderDetails(setOne, loginId);
                }
            }
            else
            {
                return "[" + cartonNo + "]:请按队列顺序扫描，请扫描[" + isN[0].PartNo + "]";
            }
        }

        public string CheckMaterial(string cartonNo, string orderNo, string isUpdate, string loginId, string UpdateNum)
        {
            List<WmsPullingOrderDetail> wmsPullingOrderDetails = repository.GetPullingOrderDetails(orderNo);
            List<WmsPullingOrderDetail> istrue = wmsPullingOrderDetails.Where(a => a.PartNo.Equals(cartonNo)).ToList();
            if (istrue.Count == 0)
            {
                return "[" + cartonNo + "]:物料号不存在，请检查!";
            }

            List<WmsPullingOrderDetail> isN = wmsPullingOrderDetails.Where(a => a.ItemStatus == 10).OrderBy(b => b.WorkOrderSeq).ToList();
            //判断未完成的列表，扫描的物料是不是第一个
            if (isN[0].PartNo == cartonNo)
            {
                if (isUpdate == "True")
                {
                    int Num = Convert.ToInt32(UpdateNum);
                    if (Num > isN.Count)
                    {
                        return "[" + UpdateNum + "]:数量超过订单序列，请检查!";
                    }
                    bool isok = true;
                    List<WmsPullingOrderDetail> setOne = new List<WmsPullingOrderDetail>();
                    for (int i = 0; i < Num; i++)
                    {
                        if (isN[i].PartNo != cartonNo)
                        {
                            isok = false;
                        }
                        setOne.Add(isN[i]);
                    }
                    if (isok)
                    {
                        return repository.UpdatePullingOrderDetails(setOne, loginId);
                    }
                    else
                    {
                        return "[" + cartonNo + "]物料连续数量不足[" + UpdateNum + "]，请检查!";
                    }
                }
                else
                {
                    //无数量输入，默认一个
                    List<WmsPullingOrderDetail> setOne = new List<WmsPullingOrderDetail>();
                    setOne.Add(isN[0]);
                    return repository.UpdatePullingOrderDetails(setOne, loginId);
                }
            }
            else
            {
                return "[" + cartonNo + "]:请按队列顺序扫描，请扫描[" + isN[0].PartNo + "]";
            }
        }

        public string SetMoveOrderClose(string order)
        {
            return repository.SetMoveOrderClose(order);
        }


        /// <summary>
        /// 领料上线（二步）扫描周转箱或箱条码
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="orderNo"></param>
        /// <param name="cartonNo"></param>
        /// <param name="isSplit"></param>
        /// <returns></returns>
        public SetObjectDetail CheckOnlineRackOrcartNo(string loginId, string orderNo, string cartonNo, string locate)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            //配料列表
            List<WmsMoveDetail_Extend> detailInfo = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);
            //获取单据的物料信息
            //List<WmsMoveDetail_Extend> detailInfo = repository.GetMoveDetailInfo(orderNo);

            //判断扫码的是箱条码还是周转箱
            SysStock stockInfo = repository.GetStockCartonInfo(cartonNo);
            if (stockInfo != null && stockInfo.CartonType == 1)
            {
                //获取周转箱中的物料
                List<WmsRackPackage_Extend> rackPackageInfo = repository.GetRackPackageInfo(cartonNo);

                //获取单据内的条码
                List<WmsMoveSn> moveSnlist = repository.GetMoveSnList(orderNo);

                //验证周转箱中物料与单据内物料中move_sn中的条码是否一致
                for (int i = 0; i < moveSnlist.Count; i++)
                {
                    List<WmsRackPackage_Extend> wmsRacks = rackPackageInfo.Where(x => x.CortonNo == moveSnlist[i].CartonNo).ToList();
                    if (wmsRacks.Count == 0)
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + cartonNo + "]:周转箱中的物料条码与单据内不一致，请检查!";
                        return rfInfo;
                    }
                }
                List<SysLocate> locateInfo = repository.GetLocateInfo(locate);

                //将周转箱中的物料条码改变为线边库位
                repository.UpdteOnlineStatus(rackPackageInfo, detailInfo, moveSnlist, orderNo, locateInfo.FirstOrDefault().LocateId, locateInfo.FirstOrDefault().LocateName, cartonNo, loginId);


                if (rackPackageInfo.Count > 0)
                {
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + cartonNo + "]:操作成功!";
                    rfInfo.wmsObjectDetail = rackPackageInfo.ConvertAll(s => (object)s);
                }
                else
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                }
            }
            else
            {
                //上料列表
                List<WmsMoveSn> details = repository.GetMoveSN(orderNo);
                //扫描到的箱条码
                SysStock Stock = repository.GetCartonInfo(cartonNo);
                //库位
                List<SysLocate> locateInfo = repository.GetLocateInfo(locate);
                if (Stock == null)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                    return rfInfo;
                }
                if (Stock.Enabled.Equals("N"))
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码已被冻结，请检查!";
                    return rfInfo;
                }

                if (Stock.Status == (int)WmsEnumUtil.StockStatus.ONLINED)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码已完成上线!";
                    return rfInfo;
                }

                if (Stock.Status != (int)WmsEnumUtil.StockStatus.BATCHED)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码不处于配料状态!";
                    return rfInfo;
                }

                List<WmsMoveSn> ifOnline = details.Where(a => a.CartonNo == cartonNo).ToList();
                if (ifOnline.Count == 0)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:不在领料单中，请检查！";
                    return rfInfo;
                }
                if (ifOnline[0].FactoryId != Stock.FactoryId)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:与领料单不在同工厂，请检查！";
                    return rfInfo;
                }
                if (ifOnline[0].PartNo != locateInfo[0].PartNo)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + ifOnline[0].PartNo + "]零件与[" + locate + "]不匹配，请检查！";
                    return rfInfo;
                }

                rfInfo.type = "PASS";
                rfInfo.message = "[" + cartonNo + "]:操作成功!";
                repository.UpdteStatusOnLine(loginId, orderNo, Stock, locateInfo);
                return rfInfo;
            }

            return rfInfo;
        }

    }
}
